package com.bdqn.service.impl;

import com.bdqn.mapper.HisRegistrationMapper;
import com.bdqn.mapper.HisSchedulingMapper;
import com.bdqn.mapper.SysDeptMapper;
import com.bdqn.mapper.UserMapper;
import com.bdqn.pojo.HisRegistration;
import com.bdqn.pojo.HisScheduling;
import com.bdqn.pojo.SysDept;
import com.bdqn.pojo.SysRoleUser;
import com.bdqn.service.HisRegistrationService;
import com.bdqn.service.SysDeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDeptServiceImpl implements SysDeptService {
    @Autowired
    SysDeptMapper sysDeptMapper;
    @Autowired
    HisSchedulingMapper hisSchedulingMapper;
    @Autowired
    HisRegistrationMapper hisRegistrationMapper;
    @Autowired
    UserMapper userMapper;


    @Override
    public List<SysDept> getSysDept() {
        return sysDeptMapper.getSysDept();
    }

    @Override
    public SysDept getSysDeptID(int deptId) {
        return sysDeptMapper.getSysDeptID(deptId);
    }

    @Override
    public PageInfo<SysDept> getSysDeptFY(int pageNum,int pageSize,int deptId,int subsectionType,int schedulingType,String schedulingDay) {
        String riqi="";
        if (schedulingDay.length()!=0){
            riqi=schedulingDay.substring(0,10);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<SysDept> list=sysDeptMapper.getSysDeptFY(deptId);
        for (SysDept sysDept:list){
            sysDept.setHisRegistrations(hisRegistrationMapper.getHisRegistrationKS(sysDept.getDeptId()));
            sysDept.setGuahao(10-sysDept.getHisRegistrations().size());
        }
        for (SysDept sysDept:list){
            List<HisScheduling> hisSchedulings=hisSchedulingMapper.getHisSchedulingPB(sysDept.getDeptId(),subsectionType,schedulingType,riqi);
            for (HisScheduling hisScheduling:hisSchedulings){
                hisScheduling.setUser(userMapper.getUserDANGE(hisScheduling.getUserId()));
            }
            sysDept.setHisScheduling(hisSchedulings);
//            System.out.println(hisSchedulings);
        }
        PageInfo<SysDept> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<SysDept> getPage(int pageNum, int pageSize, String deptName, int status) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysDept> list=sysDeptMapper.getPage(deptName,status);
        PageInfo<SysDept> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int addDept(String deptName, String deptNumber, String deptLeader, String leaderPhone, int orderNum, int status, String userName) {
        return sysDeptMapper.addDept(deptName,deptNumber,deptLeader,leaderPhone,orderNum
                ,status,userName);
    }

    @Override
    public int setDept(String deptName, String deptNumber, String deptLeader, String leaderPhone, int orderNum, int status, String userName, String deptId) {
        return sysDeptMapper.setDept(deptName,deptNumber,deptLeader,leaderPhone,orderNum
                ,status,userName,deptId);
    }

    @Override
    public int delDept(String deptId) {
        return sysDeptMapper.delDept(deptId);
    }
}
